//
//  String+Size.swift
//  SK_BasePrivateProject
//
//  Created by Chenjing on 2018/7/5.
//  Copyright © 2018年 leapp. All rights reserved.
//

import Foundation
import UIKit

extension String {
    
    /**
     *  计算字符串高度
     *
     *  @param  width 最大高度
     *  @param  font  字体大小，默认为系统字体大小
     *
     *  @return 字符串高度
     */
    func height(width: CGFloat, font: UIFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)) -> CGFloat {
        
        let rect: CGRect = (self as NSString).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)

        return rect.size.height
    }
    
    /**
     *  计算字符串宽度
     *
     *  @param  height 最大宽度
     *  @param  font  字体大小，默认为系统字体大小
     *
     *  @return 字符串高度
     */
    func width(height: CGFloat, font: UIFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)) -> CGFloat {
        
        let rect: CGRect = (self as NSString).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)
        
        return rect.size.height
    }
    
    /**
     *  计算字符串Size
     *
     *  @param  size 最大Size
     *  @param  font  字体大小，默认为系统字体大小
     *
     *  @return 字符串Size
     */
    func constrainedSize(size: CGSize, font: UIFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)) -> CGSize {
        
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineBreakMode = .byWordWrapping
        
        let size: CGSize = (self as NSString).boundingRect(with: size, options: .truncatesLastVisibleLine, attributes: [NSAttributedStringKey.font: font, NSAttributedStringKey.paragraphStyle: paragraph], context: nil).size
        
        return CGSize.init(width: ceil(size.width), height: ceil(size.height))
    }
}

extension NSString {
    
    /**
     *  计算字符串高度
     *
     *  @param  width 最大高度
     *  @param  font  字体大小，默认为系统字体大小
     *
     *  @return 字符串高度
     */
    func height(width: CGFloat, font: UIFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)) -> CGFloat {
        
        let rect: CGRect = self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)
        
        return rect.size.height
    }
    /**
     *  计算字符串宽度
     *
     *  @param  height 最大宽度
     *  @param  font  字体大小，默认为系统字体大小
     *
     *  @return 字符串高度
     */
    func width(height: CGFloat, font: UIFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)) -> CGFloat {
        
        let rect: CGRect = self.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)
        
        return rect.size.height
    }
    /**
     *  计算字符串Size
     *
     *  @param  size 最大Size
     *  @param  font  字体大小，默认为系统字体大小
     *
     *  @return 字符串Size
     */
    func constrainedSize(size: CGSize, font: UIFont = UIFont.systemFont(ofSize: UIFont.systemFontSize)) -> CGSize {
        
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineBreakMode = .byWordWrapping
        
        let size: CGSize = self.boundingRect(with: size, options: .truncatesLastVisibleLine, attributes: [NSAttributedStringKey.font: font, NSAttributedStringKey.paragraphStyle: paragraph], context: nil).size
        
        return CGSize.init(width: ceil(size.width), height: ceil(size.height))
    }
}

extension NSAttributedString {
    
    /**
     *  计算富文本高度
     *
     *  @param  width 最大宽度
     *
     *  @return 字符串高度
     */
    func height(width: CGFloat) -> CGFloat {
        
        let rect: CGRect = self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, context: nil);
        
        return rect.size.height
    }
    /**
     *  计算富文本宽度
     *
     *  @param  width 最大高度
     *
     *  @return 字符串高度
     */
    func width(height: CGFloat) -> CGFloat {
        
        let rect: CGRect = self.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, context: nil)
        
        return rect.size.height
    }
    /**
     *  计算富文本Size
     *
     *  @param  size 最大Size
     *
     *  @return 富文本Size
     */
    func constrainedSize(size: CGSize) -> CGSize {
        
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineBreakMode = .byWordWrapping
        
        let size: CGSize = self.boundingRect(with: size, options: .truncatesLastVisibleLine, context: nil).size
        
        return CGSize.init(width: ceil(size.width), height: ceil(size.height))
    }
}







